<template>
  <div class="h-full flex flex-col p-2">
    <!-- 按键 -->
    <div class="flex justify-between rounded items-center bg-white p-2">
      <el-space>
        <el-button class="mr-2" type="primary" :icon="Back" @click="BackPage()"
          >返回</el-button
        >
        <!-- 选择 -->
        <el-text size="large"
          >当前路径: {{ storageService.sessionData.PicturePath }}</el-text
        >

        <el-button
          class="ml-2 mr-2"
          type="primary"
          :icon="Upload"
          @click="() => (show.uploading = true)"
          >上传图片
        </el-button>

        <el-popconfirm
          width="240"
          title="此操作将永久删除,是否继续?"
          @confirm="DeleteFilePicture()"
        >
          <template #reference>
            <el-button
              class="mr-2"
              type="danger"
              plain
              :icon="Delete"
              :disabled="mForbiddenState"
              >批量删除</el-button
            >
          </template>
        </el-popconfirm>

        <el-popconfirm
          width="240"
          title="此操作将永久删除,是否继续?"
          @confirm="OneClickDelete()"
        >
          <template #reference>
            <el-button
              type="danger"
              :icon="Delete"
              webkitdirectory
              directory
              multiple
              >一键删除</el-button
            >
          </template>
        </el-popconfirm>

        <el-checkbox
          class="pl-2"
          v-model="mSelectAll"
          label="全选"
          size="large"
          @change="SelectAllEventTrigger()"
        />
      </el-space>

      <el-space class="flex text-red-500"
        >温馨提示：删除训练数据集资源后不可恢复请谨慎进行删除操作！
      </el-space>
    </div>
    <!-- 数据集列表 -->
    <div
      class="flex flex-col rounded pb-2"
      style="width: 100%; height: calc(100vh - 11.5rem)"
    >
      <el-row :gutter="10" class="scroll-container">
        <el-col :span="4" v-for="(item, index) in tableData" :key="item">
          <div class="bg-white rounded w-full mt-2 justify-center items-center">
            <el-image
              ref="imageRef"
              class="mt-2 w-full rounded h-44"
              :src="'http://192.168.1.121:5000/datasets/' + item.filePath"
              :preview-src-list="srcList"
              :initial-index="index"
              fit="contain"
            />
            <!-- 备注 -->
            <el-tooltip effect="light" :content="item.fileName" placement="top">
              <h5 class="truncate-text">
                {{ item.fileName }}
              </h5>
            </el-tooltip>

            <el-divider />

            <div class="grid grid-cols-2 w-full">
              <el-checkbox
                class="w-full h-10 grid justify-center items-center border cursor-pointer transition-all duration-300 hover:shadow-lg active:scale-95 hover:bg-gray-100"
                v-model="item.selected"
                size="large"
                @change="SelectionEventTrigger()"
              />

              <el-popconfirm
                width="240"
                title="此操作将永久删除,是否继续?"
                @confirm="DeleteFilePicture(item)"
              >
                <template #reference>
                  <div
                    class="w-full h-10 grid justify-center items-center cursor-pointer transition-all duration-300 hover:shadow-lg active:scale-95 hover:bg-gray-100"
                  >
                    <el-icon color="#FF0000" :size="20">
                      <Delete />
                    </el-icon>
                  </div>
                </template>
              </el-popconfirm>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>
    <!-- 分页 -->
    <div class="h-12 flex rounded items-center justify-end bg-white">
      <el-pagination
        :current-page="pagination.pageNumber"
        :page-size="pagination.articleNumber"
        :page-sizes="[10, 20, 30, 40, 50]"
        layout="total, sizes, prev, pager, next"
        :total="pagination.altogether"
        @current-change="onCurrentChange"
        @size-change="onSizeChange"
      />
    </div>
    <!-- 弹框 -->
    <el-dialog
      v-model="show.uploading"
      title="上传文件"
      width="30%"
      destroy-on-close
      :before-close="handleBeforeClose"
    >
      <UploadFileDialog></UploadFileDialog>
    </el-dialog>
  </div>
</template>
<script setup>
//
import { onMounted, reactive, ref } from "vue";
//
import { Back, Upload, Delete, View } from "@element-plus/icons-vue";
/**
 * 存储
 */
import storageService from "@/utils/dataStore_utils";
/**
 * 上传文件
 */
import UploadFileDialog from "@/components/dialog/UploadFileDialog.vue";
/**
 * 训练集
 */
import FileUploadApi from "@/api/modules/FileUploadApi";
/**
 *
 */
import { requestedMessage } from "@/utils/requested_utils";

/**
 *  禁止状态
 */
const mForbiddenState = ref(true);
/**
 * 数据全选
 */
const mSelectAll = ref(false);

/**
 * 展示弹框
 */
const show = reactive({
  uploading: false,
});
/**
 * 分页
 */
const pagination = reactive({
  pageNumber: 1,
  articleNumber: 10,
  altogether: 0,
});
/**
 * 回数据
 */
const tableData = ref([]);
/**
 * 属性
 */
const imageRef = ref(null);
/**
 *  图片RUL
 */
const srcList = ref([]);
/**
 *
 */
const collectionIds = ref();

/**
 * 生命周期
 */
onMounted(() => {
  /**
   * 获取训练集列表
   */
  getList();
});

/**
 *  获取训练集列表
 */
function getList() {
  requestedMessage(
    async () => {
      const { message, msg } = await FileUploadApi.getResourceList({
        pageIndex: pagination.pageNumber,
        pageSize: pagination.articleNumber,
        trainingSetId: storageService.sessionData.TheDataSetid,
        pre: storageService.sessionData.PicturePath,
      });
      tableData.value = message.list;
      pagination.altogether = message.total;
      /**
       * 清理
       */
      srcList.value = [];
      /**
       * 循环
       */
      for (var i = 0; i < tableData.value.length; i++) {
        srcList.value.push(
          "http://192.168.1.121:5000/datasets/" + tableData.value[i].filePath
        ); // 将每一项添加到 srcList 数组中
      }

      return msg;
    },
    {
      showSuccess: true,
      fullscreenLoading: true,
    }
  );
}

/**
 * 返回
 */
function BackPage() {
  /**
   * 返回数据
   */
  window.$router.push("/data_catalogue");
}

/**
 * 一键删除
 */
function OneClickDelete() {
  requestedMessage(
    async () => {
      const { message, msg } = await FileUploadApi.deleteAllResource(
        storageService.sessionData.TheDataSetid
      );
      /**
       * 获取训练集列表
       */
      getList();
      /**
       *
       */
      return msg;
    },
    {
      showSuccess: true,
      fullscreenLoading: true,
    }
  );
}

/**
 * 全选事件触发
 */
function SelectAllEventTrigger() {
  for (var i = 0; i < tableData.value.length; i++) {
    tableData.value[i].selected = mSelectAll.value;
  }
}

/**
 * 选择事件触发
 */
function SelectionEventTrigger() {
  /**
   * 是否禁止状态
   */
  var DisabledOrNot = true;
  /**
   *
   */
  let selectedItems = [];
  /**
   * 循环
   */
  for (var i = 0; i < tableData.value.length; i++) {
    if (tableData.value[i].selected) {
      DisabledOrNot = false;
      selectedItems.push(tableData.value[i].collectionId); // 替换 someProperty 为实际属性名
    }
  }
  /**
   *
   */
  mForbiddenState.value = DisabledOrNot;
  /**
   *
   */
  collectionIds.value = selectedItems;
}

/**
 * 删除文件图片
 */
function DeleteFilePicture(item) {
  requestedMessage(
    async () => {
      const { message, msg } = await FileUploadApi.batchDeleteResource(
        item === undefined ? collectionIds.value : item.collectionId
      );
      /**
       * 获取训练集列表
       */
      getList();
      /**
       *
       */
      return msg;
    },
    {
      showSuccess: true,
      fullscreenLoading: true,
    }
  );
}

/**
 *  分页调用
 */
function onCurrentChange(num) {
  pagination.pageNumber = num;
  getList();
}
/**
 *  分页调用
 */
function onSizeChange(num) {
  pagination.pageNumber = 1;
  pagination.articleNumber = num;
  getList();
}

/**
 * Dialog 关闭动画结束时的回调
 */
function handleBeforeClose(done) {
  /**
   *
   */
  show.uploading = false;
  /**
   *
   */
  getList();
}
</script>

<!--  -->
<style>
.scroll-container {
  display: flex;
  overflow-x: auto; /* 启用水平滚动 */
  -webkit-overflow-scrolling: touch; /* 在移动设备上启用惯性滚动 */
}

/* 滚动条整体样式 */
::-webkit-scrollbar {
  width: 6px; /* 滚动条宽度 */
  height: 10px; /* 垂直滚动条高度（如果需要垂直滚动条的话） */
}

/* 滚动条轨道样式 */
::-webkit-scrollbar-track {
  background: #f1f1f1; /* 轨道颜色 */
  border-radius: 6px; /* 圆角 */
}

/* 滚动条滑块样式 */
::-webkit-scrollbar-thumb {
  background: #888; /* 滑块颜色 */
  border-radius: 3px; /* 圆角 */
  border: 1px solid #f1f1f1; /* 滑块边框（实际上是轨道颜色的延伸，用于创建内边距效果） */
}

/* 滑块悬停样式 */
::-webkit-scrollbar-thumb:hover {
  background: #555; /* 悬停时滑块颜色 */
}

h5 {
  margin-block-start: 0.9em;
  margin-block-end: 1em;
}

.truncate-text {
  white-space: nowrap; /* 防止文本换行 */
  overflow: hidden; /* 隐藏溢出的文本 */
  text-overflow: ellipsis; /* 当文本溢出时显示省略号 */
  text-align: center;
  max-width: 100%; /* 确保文本容器有明确的宽度限制 */
  padding: 0 8px; /* 可选：添加一些内边距让文本看起来更舒适 */
}

.el-divider--horizontal {
  border-top: 1px var(--el-border-color) var(--el-border-style);
  display: block;
  height: 1px;
  margin: 0px 0;
  width: 100%;
}
</style>
